import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/device/device_const.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';
import 'package:sprintf/sprintf.dart';

import 'sleep_setting_logic.dart';

class SleepSettingPage extends StatelessWidget {
  SleepSettingPage({super.key});

  final logic = Get.put(SleepSettingLogic());
  final state = Get.find<SleepSettingLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(LocaleKeys.settings_sleep.ts),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back())
      ),
      body: Padding(
          padding: EdgeInsets.only(left: 40.w, top: 80.h, right: 40.w),
          child: ListView(
            children: [
              Container(
                height: 175.h,
                padding: EdgeInsets.only(left: 59.w, right: 36.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20),
                ),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        LocaleKeys.settings_spo2_enable.ts,
                        style: TextStyle(
                            fontSize: 42.sp,
                            color: ColorConst.primaryFontColor),
                      ),
                      Transform.scale(
                          scale: 0.7,
                          child: Obx(() => CupertinoSwitch(
                              value: state.isSciSleep.value,
                              activeColor: ColorConst.accentColor,
                              onChanged: (value) {
                                logic.setSleepEnable(value);
                              })))
                    ]),
              ),
              SizedBox(height: 75.h),
              /// 时间段sdk不支持，暂时屏蔽
              // _buildSleepTimeWidget(context)
            ],
          )),
    );
  }

  Widget _buildSleepTimeWidget(BuildContext context) {
    return InkWell(
      child: Container(
        height: 247.h,
        padding: EdgeInsets.only(left: 59.w, top: 48.h, right: 36.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        child: Obx(()=>Column(
          children: [
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    LocaleKeys.settings_time_slot.ts,
                    style: TextStyle(
                        fontSize: 42.sp,
                        color: ColorConst.primaryFontColor),
                  ),
                  Expanded(child: Container()),
                  Text(
                    sprintf(LocaleKeys.settings_time_slot_subtitle.ts,
                        [state.startTime.value, state.endTime.value]),
                    style: TextStyle(
                        fontSize: 42.sp,
                        color: ColorConst.primaryFontColor),
                  ),
                  SizedBox(width: 40.w),
                  const Icon(
                    Icons.arrow_forward_ios,
                    size: 14,
                  ),
                ]),
            SizedBox(height: 30.h),
            Container(
              alignment: Alignment.centerLeft,
              child: Text(
                LocaleKeys.settings_time_slot_tips.ts,
                style: TextStyle(
                    fontSize: 36.sp,
                    color: ColorConst.secondaryFontColor),
              ),
            ),
          ],
        )),
      ),
      onTap: () async {
        var result = await Get.toNamed("/device/settings/time_slot",
            arguments: {DeviceConst.keyStartTime: state.startTime.value,
              DeviceConst.keyEndTime: state.endTime.value}
        );

        if (result[DeviceConst.changed]) {
          state.startTime.value = result[DeviceConst.keyStartTime];
          state.endTime.value = result[DeviceConst.keyEndTime];
        }
      },
    );
  }
}
